package com.liu.network.httpclient;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.goodday.common.constant.ConstantUtil;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;

public class HtmlParserByJsoup {
	private int dataType;
	private String url;

	public HtmlParserByJsoup(String url, int dataType) {
		this.dataType = dataType;
		this.url = url;
	}

/**
 * 	
 * @return
 */
	public String htmlParser() {
		Document doc = null;
		String result = new String();
		try {
			doc = getResponseData();
			result = doc.toString();
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result.toString();
	}

	
	/**
	 * 获取url响应数据
	 * 
	 * @return
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	private Document getResponseData()
			throws FailingHttpStatusCodeException, MalformedURLException, IOException {
		Document doc = null;
		switch (dataType) {
		case ConstantUtil.DYNAMIC_DATA:
			// 屏蔽HtmlUnit等系统 log
			LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log",
					"org.apache.commons.logging.impl.NoOpLog");
			java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
			java.util.logging.Logger.getLogger("org.apache.http.client").setLevel(Level.OFF);

			// 新建一个模拟谷歌Chrome浏览器的浏览器客户端对象
			WebClient browser = new WebClient(BrowserVersion.CHROME);
			// 启用JS解释器，默认为true
			browser.getOptions().setJavaScriptEnabled(true);
			// 禁用css支持
			browser.getOptions().setCssEnabled(false);
			// js运行错误时，是否抛出异常
			browser.getOptions().setThrowExceptionOnScriptError(false);
			// 当HTTP的状态非200时是否抛出异常, 这里选择不需要
			browser.getOptions().setThrowExceptionOnFailingStatusCode(false);
			browser.getOptions().setRedirectEnabled(true);
			// 设置连接超时时间
			browser.getOptions().setTimeout(10 * 1000);

			// 获取页面
			HtmlPage htmlPage = browser.getPage(url);
			// 设置等待js的加载时间
			browser.waitForBackgroundJavaScript(30 * 1000);

			// 使用xml的方式解析获取到jsoup的document对象
//						doc = Jsoup.parse(htmlPage.asXml(), getUrl);
			doc = Jsoup.parse(htmlPage.asXml());
			break;
		case ConstantUtil.STATIC_DATA:
			doc = Jsoup.connect(url).timeout(500000).get();
			break;
		default:
			doc = Jsoup.connect(url).timeout(500000).get();
			break;
		}
		return doc;
	}

}
